package com.example.tellwin;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.mine.bean.UserBean;
import com.example.tellwin.mine.bean.UserInfo;
import com.google.gson.Gson;

/**
 * @author : LinZePeng
 * @date : 2020/11/25
 * @Describe ：
 **/
public class AppHelper {

    private static AppHelper sInstance = null;

    private UserBean userBean;
    private UserInfo userInfo;

    private String shareType;
    private String shareId;

    public static AppHelper getInstance() {
        if (null == sInstance) {
            synchronized (AppHelper.class) {
                if (null == sInstance) {
                    sInstance = new AppHelper();
                }
            }
        }
        return sInstance;
    }

    public void init(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("User", Activity.MODE_PRIVATE);
        String json = sharedPreferences.getString("User", "");
        if (TextUtils.isEmpty(json)) {
            userBean = null;
        } else {
            userBean = new Gson().fromJson(json, UserBean.class);
        }
        LogUtils.d(userBean);
    }

    public void saveUser(UserBean userBean, Context context) {
        this.userBean = userBean;
        SharedPreferences sharedPreferences = context.getSharedPreferences("User", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("User", new Gson().toJson(userBean));
        editor.commit();
    }

    public String getShareType() {
        return shareType;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void setShareType(String shareType) {
        this.shareType = shareType;
    }

    public String getShareId() {
        return shareId;
    }

    public void setShareId(String shareId) {
        this.shareId = shareId;
    }


    public void clearUser(Context context) {
        userBean = null;
        SharedPreferences sharedPreferences = context.getSharedPreferences("User", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }

    public UserBean getUser() {
        return userBean;
    }

    public String getUserId() {

        if (userBean != null) {
            if (!TextUtils.isEmpty(userBean.getAnswerUserId())) {
                return userBean.getAnswerUserId();
            }
            return userBean.getUserId();
        }
        return "";
    }

    public String getToken() {
        LogUtils.e(userBean);
        if (userBean != null) {
            if (!TextUtils.isEmpty(userBean.getAnswerToken())) {
                return userBean.getAnswerToken();
            }
            return userBean.getToken();
        }
        return "";
    }

    public boolean isLogin() {
        return userBean != null && (!TextUtils.isEmpty(userBean.getToken()) || !TextUtils.isEmpty(userBean.getAnswerToken()));
    }

    public boolean isAnswer() {
        if (userBean == null) {
            return false;
        }
        return !TextUtils.isEmpty(userBean.getAnswerUserId()) && !TextUtils.isEmpty(userBean.getAnswerToken());
    }

}
